package com.itzyf.pokemondata.activity.illustration

import android.text.TextUtils
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbCharacteristicDo
import com.itzyf.pokemondata.bean.DbSkillDo
import com.itzyf.pokemondata.bean.DbSuperSkillDo
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2019/04/29 19:36
 */
class CharacteristicActivity : BaseActivity() {
    var mDatas: MutableList<DbCharacteristicDo?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter
    var index = 0

    override fun handlerActivity() {
        mAdapter = QuickAdapter(R.layout.item_characteristic, mDatas)
        rv_list.adapter = mAdapter

        rv_list.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })
        mSearchView.hint = "请输入特性名称"
        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })
//        mAdapter.setOnItemChildClickListener { _, view, position ->
//            if (view.id == R.id.tvDetail) {
//                Log.d("AVOS",mDatas[position]!!.detail)
//                showDetail(mDatas[position]!!.detail)
//            }
//        }
        queryDatas()
    }

    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where = "cname like '%$query%'"
        }
        val dataList =
            LitePal
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(DbCharacteristicDo::class.java)

        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            mDatas.add(obj)
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<DbCharacteristicDo?>) :
        BaseQuickAdapter<DbCharacteristicDo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: DbCharacteristicDo?) {
            helper.setText(R.id.tvName, item?.cname)
            helper.setText(R.id.tvEffect, item?.explain)
//            helper.setVisible(R.id.tvDetail, !TextUtils.isEmpty(item?.detail))
            item?.let {
                helper.setText(R.id.tvDetail, item.detail)
                if (TextUtils.isEmpty(it.detail)) {
                    helper.setGone(R.id.tvDetail, false)
                    helper.setGone(R.id.llDetail, false)
                }else{
                    helper.setGone(R.id.tvDetail, true)
                    helper.setGone(R.id.llDetail, true)
                }
            }
//            helper.addOnClickListener(R.id.tvDetail)
        }
    }


    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "特性图鉴"

//    private fun showDetail(msg: String) {
//        QMUIDialog.MessageDialogBuilder(this)
//            .setTitle("详情")
//            .setMessage(msg.replace("\\n","\n"))
//            .addAction(
//                "确定"
//            ) { dialog, _ ->
//                dialog.dismiss()
//            }
//            .show()
//    }

}